package com.ruoyi.tt.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.tt.domain.dto.ArticleSearchDto;
import com.ruoyi.tt.domain.vo.ArticleDetailVO;
import com.ruoyi.tt.domain.vo.ArticleVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tt.domain.TtArticle;
import com.ruoyi.tt.service.ITtArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章管理Controller
 *
 * @author 梁静茹
 * @date 2022-10-17
 */
@RestController
@RequestMapping("/tt/article")
public class TtArticleController extends BaseController {
    @Autowired
    private ITtArticleService ttArticleService;


    /**
     * 根据标题的关键字进行模糊查询
     *
     * @param articleSearchDto
     * @return
     */
    @PostMapping("getArticleByKeyWord")
    public AjaxResult getArticleByKeyWord(@RequestBody ArticleSearchDto articleSearchDto) {
        System.out.println("articleSearchDto = " + articleSearchDto);
        List<ArticleVO> list = ttArticleService.getArticleByKeyWord(articleSearchDto);
        return AjaxResult.success(list);
    }

    /**
     * 根据文章id，获取文章详情&评论列表
     *
     * @param articleId
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/getArticleDetailByArticleId/{articleId}")
    public AjaxResult getArticleDetailByArticleId(@PathVariable("articleId") Integer articleId) throws InterruptedException {
        ArticleDetailVO articleDetailVO = ttArticleService.getArticleDetailByArticleId(articleId);
        AjaxResult result = AjaxResult.success("成功", articleDetailVO);
        return result;

    }

    /**
     * 查询文章管理列表
     */
    @GetMapping("/listByTypeId")
    public TableDataInfo getListByTypeId(@RequestParam("typeId") Integer typeId, @RequestParam("time") String time
    ) throws InterruptedException {
//        Long userId = SecurityUtils.getUserId();
//        System.out.println("user  Id = " + userId);
//        Thread.sleep(300);  上线
        System.out.println("time = " + time);
//        startPage();
        List<ArticleVO> list = ttArticleService.getListByTypeId(typeId, time);
        // 这里查出来的数据没有问题
        return getDataTable(list);
    }

    /**
     * 查询文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('tt:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(TtArticle ttArticle) {
        startPage();
        List<TtArticle> list = ttArticleService.selectTtArticleList(ttArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('tt:article:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TtArticle ttArticle) {
        List<TtArticle> list = ttArticleService.selectTtArticleList(ttArticle);
        ExcelUtil<TtArticle> util = new ExcelUtil<TtArticle>(TtArticle.class);
        util.exportExcel(response, list, "文章管理数据");
    }

    /**
     * 获取文章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('tt:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ttArticleService.selectTtArticleById(id));
    }

    /**
     * 新增文章管理
     */
    @PreAuthorize("@ss.hasPermi('tt:article:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TtArticle ttArticle) {
        return toAjax(ttArticleService.insertTtArticle(ttArticle));
    }

    /**
     * 修改文章管理
     */
    @PreAuthorize("@ss.hasPermi('tt:article:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TtArticle ttArticle) {
        return toAjax(ttArticleService.updateTtArticle(ttArticle));
    }

    /**
     * 删除文章管理
     */
    @PreAuthorize("@ss.hasPermi('tt:article:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ttArticleService.deleteTtArticleByIds(ids));
    }
}
